R Markdown

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.2
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## Warning: package 'tibble' was built under R version 4.1.3
## Warning: package 'tidyr' was built under R version 4.1.3
## Warning: package 'readr' was built under R version 4.1.3
## Warning: package 'dplyr' was built under R version 4.1.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggplot2)
library(plotly)
## Warning: package 'plotly' was built under R version 4.1.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
(df <- read_csv(here::here("Results", 'Seaon Total Innings.csv')))
## Rows: 150292 Columns: 7
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## dbl (7): Inning, Home Team, First, Second, Third, Outs, Inning Runs
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
## # A tibble: 150,292 x 7
##    Inning `Home Team` First Second Third  Outs `Inning Runs`
##     <dbl>       <dbl> <dbl>  <dbl> <dbl> <dbl>         <dbl>
##  1      1           0     0      0     0     0             0
##  2      1           0     0      0     0     1             0
##  3      1           0     1      0     0     2             0
##  4      1           1     0      0     0     0             0
##  5      1           1     0      0     0     1             0
##  6      1           1     0      0     0     2             0
##  7      2           0     0      0     0     0             1
##  8      2           0     0      0     0     1             1
##  9      2           0     0      0     1     1             1
## 10      2           0     0      1     0     1             1
## # ... with 150,282 more rows
df <- df %>% 
  mutate(`Scored` = if_else(`Inning Runs` >= 1,TRUE,FALSE))


df_group <- df %>% 
  group_by(First, Second, Third, Outs) %>% 
  summarise(Runs = sum(`Inning Runs`),
         Occurences = n(),
         `Probabilty of Scoring` = mean(Scored))
## `summarise()` has grouped output by 'First', 'Second', 'Third'. You can
## override using the `.groups` argument.
df_group <- df_group %>%  
  mutate(`Runs Expected` = Runs/Occurences) %>% 
  unite(Onbase, First:Third) 

runs_expected_matrix <- df_group %>% 
  pivot_wider(id_cols=Onbase, names_from=Outs, values_from=`Runs Expected`, names_prefix='Outs: ', names_sep=' ') %>% 
  arrange(desc('Outs: 0'))
  

run_prob_matrix <- df_group %>% 
  pivot_wider(id_cols=Onbase, names_from=Outs, values_from=`Probabilty of Scoring`, names_prefix='Outs: ', names_sep=' ')


View(df_group)
View(runs_expected_matrix)
View(run_prob_matrix)
ggplot(df_group, aes(x=Onbase, y=`Runs Expected`))+
  geom_boxplot()

ggplot(df_group, aes(y=Onbase, fill=`Runs Expected`, x=Outs))+
  geom_tile()

p <- df_group %>% 
  ggplot(aes(y=Onbase, fill=`Probabilty of Scoring`, x=Outs))+
  geom_tile()
ggplotly(p)


```r
df_bases <- df %>% 
  unite(Onbase, First:Third) %>% 
  mutate(Outs = as.factor(Outs))
  
  
p <- df_bases %>% 
  ggplot(aes(x=`Inning Runs`, color=Outs))+
  geom_density()
ggplotly(p)